﻿using System.Collections.Generic;
using Crevice2D.Core.Data;
using Crevice2D.Core.Infrastructure;
using Crevice2D.Core.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace Patyk.Game
{
    public class Game : BaseGame
    {
        public static Game _this;
        protected Level Level;

        protected Camera2D _camera;

        private KeyboardState _currentKeyboardState;
        private KeyboardState _previousKeyboardState;

        private GamePadState _currentGamePadState;
        private GamePadState _previousGamePadState;

        private MouseState _currentMouseState;
        private MouseState _previousMouseState;

        public Game()
        {
            _this = this;
            _camera = new Camera2D();

            RenderManager.Initialise(this);
            TouchPanel.EnabledGestures = GestureType.FreeDrag;
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            RenderManager.LoadContent();

            _camera.Initialize();

            // Game specifics
            Level = new Level(0);
        }

        protected override void UnloadContent()
        {
            RenderManager.Content.Unload();
        }

        protected override void Update(GameTime gameTime)
        {
            // Read the current state of the keyboard and gamepad and store it
            _currentKeyboardState = Keyboard.GetState();
            _currentGamePadState = GamePad.GetState(PlayerIndex.One);
            _currentMouseState = Mouse.GetState();

            HandleInput();

            // update our level, passing down the GameTime along with all of our input states
            Level.Update(gameTime, _currentKeyboardState, _previousKeyboardState, _currentGamePadState, _previousGamePadState,
                _currentMouseState, _previousMouseState);

            // update camera to follow player 
            _camera.UpdatePosition(Level.Player.Position, Level.Player.PrevPosition);

            // Save the previous state of the keyboard and game pad so we can determine single key/button presses
            _previousGamePadState = _currentGamePadState;
            _previousKeyboardState = _currentKeyboardState;
            _previousMouseState = _currentMouseState;

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            RenderManager.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
                    null, null, null, null, _camera.TransfromMatrix);

            Level.Draw(gameTime);

            base.Draw(gameTime);

            RenderManager.SpriteBatch.End();

            DrawHUD();
        }

        public virtual void HandleInput()
        {

        }

        public virtual void DrawHUD()
        {

        }
    }
}
